﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation
{
    public class Animation : GameObject
    {
        // the animation
        protected AnimatedSprite.AnimatedTexture textureAnimator;

        //TODO: explosion sound
        protected int NUMBER_OF_FRAMES_TO_PLAY;
        protected int FRAMES_PER_SECOND;
        protected String spriteName;

        public Animation(WorldProperties worldProperties, GameObject parent, float scale, int framesToPlay, int framesPerSecond, String spriteName)
            : base(worldProperties)
        {
            // dont store the parent so it gets garbage collected

            //// make sure it loads and draws
            DrawOrder = parent.DrawOrder;
            UpdateOrder = parent.UpdateOrder;
            Visible = true;
            Enabled = true;

            NUMBER_OF_FRAMES_TO_PLAY = framesToPlay;
            FRAMES_PER_SECOND = framesPerSecond;
            this.spriteName = spriteName;

            this.boundingRectangle = new Rectangle(0, 0, 1, 1);
            this.Scale = scale;
            CentreOnObject(parent);
            Origin =parent.Origin;
            Position = parent.Position;
            //AlignOriginWithObject(parent);
            this.Rotation = parent.Rotation - MathHelper.PiOver2;
            this.Velocity = Vector2.Zero;

            LoadContent();
        }

        public Animation(WorldProperties worldProperties, Vector2 position, float rotation, Vector2 oritin, float scale, int framesToPlay, int framesPerSecond, String spriteName)
            : base(worldProperties)
        {
            //// make sure it loads and draws
            DrawOrder = 80;
            UpdateOrder = 80;
            Visible = true;
            Enabled = true;

            NUMBER_OF_FRAMES_TO_PLAY = framesToPlay;
            FRAMES_PER_SECOND = framesPerSecond;
            this.spriteName = spriteName;

            this.boundingRectangle = new Rectangle(0, 0, 1, 1);
            this.Scale = scale;
            this.Position = position;
            this.Origin = origin;
            this.Rotation = rotation;
            this.Velocity = Vector2.Zero;

            LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // is the animation done?
            if (textureAnimator.FramesUpdated > NUMBER_OF_FRAMES_TO_PLAY)
            {
                Kill();
                return;
            }

            // update the animation
            textureAnimator.UpdateFrame((float)gameTime.ElapsedGameTime.TotalSeconds);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (textureAnimator.FramesUpdated > NUMBER_OF_FRAMES_TO_PLAY)
            {
                return;
            }

            spriteBatch.Begin();
            textureAnimator.DrawFrame(spriteBatch, Position);
            spriteBatch.End();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            //prevent it from loading before the parametter are setted by the constructor
            textureAnimator = new AnimatedSprite.AnimatedTexture(Origin, Rotation, Scale, 1.0f);
                
            if (spriteName != null)
            {
               textureAnimator.Load(Game.Content, spriteName, NUMBER_OF_FRAMES_TO_PLAY, FRAMES_PER_SECOND);
            }
        }
    }
}